#coding=utf-8

import urllib2
from BeautifulSoup import BeautifulSoup

url = "http://www.daomubiji.com/dao-mu-bi-ji-quan-ji/"
user_agent = 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;\
SV1; CNCDialer; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)'
headers={'User-Agent':user_agent}
req = urllib2.Request(url)
try:
    res = urllib2.urlopen(req)
except urllib2.URLError,e:
    print e.reason
f = open("c:\\url.txt","wb")
soup = BeautifulSoup(res)
divContent = soup.find('div',{'class':'content'})
divSoup = BeautifulSoup(str(divContent.renderContents()), fromEncoding="UTF-8")
liAll = divSoup.findAll('li')
for li in liAll:
    a = li.a
    if str(a) != 'None':
        f.write(a.renderContents())
        f.write('$')
        f.write(a['href'])
        f.write("\r\n")
f.close()
print "finish"
